package com.tensquare.friend.controller;

import com.google.common.annotations.VisibleForTesting;
import com.tensquare.friend.service.FriendService;
import entity.Result;
import entity.StatusCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IDEA
 * author:zhiXiang-Chen
 * Date:2019/3/7
 * Time:15:50
 */
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendService friendService;

    //添加好友/非好友
    @RequestMapping(value = "/like/{friendid}/{type}",method = RequestMethod.PUT)
    public Result addFriend(@PathVariable(value = "friendid") String friendid, @PathVariable(value = "type") String type, HttpServletRequest request){
//        Claims claims = (Claims) request.getAttribute("user_claims");
//        String id = claims.getId();
//        if (claims==null){
//            return  new Result(false, StatusCode.ACCESSERROR, "权限不足!");
//        }

        String id ="1";
        if ("1".equals(type)){
            int addResult = friendService.addFriend(id,friendid);
            if (addResult==0){
                return new Result(true, StatusCode.ERROR, "该好友已经添加过", null);
            }

        }else {
            //添加非好友
            friendService.addNoFriend(id,friendid);
        }
        return new Result(true, StatusCode.OK, "操作成功", null);
    }

    /**
     * 删除好友
     * @param friendid
     * @return
     */
    @RequestMapping(value ="/{friendid}" ,method = RequestMethod.DELETE)
    public Result deleteFriend(@PathVariable(value = "friendid") String friendid,HttpServletRequest request) {
//        Claims claims = (Claims) request.getAttribute("user_claims");
       // String id = claims.getId();
//        if(claims == null){
//            return  new Result(false, StatusCode.ACCESSERROR, "权限不足!");
//        }
        String id ="1";
        //调用页业务,删除好友
        friendService.deleteFriend(id,friendid);


        return new Result(true, StatusCode.OK, "删除成功");
    }



}
